package com.cheng.controller;

import com.cheng.pojo.Product;
import com.cheng.pojo.Product_team;
import com.cheng.pojo.Users;
import com.cheng.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/font")
public class font_list {

    @Autowired
    private ProductService productService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private UserService userService;

    @Autowired
    private ProductTeamService productTeamService;


    @Autowired
    @Qualifier("OrderServiceImpl")
    private OrderService orderService;


    //商品主页
    @RequestMapping("/travel")
    public String  travel(String priductaddress,
                          String priductaddress01,
                          String priductaddress02,
                          String priductaddress03,
                          Model model,
                          HttpSession session){

        if (session.getAttribute("users")==null){
            return "userLogin";
        }

        PageInfo<Product> pageInfo = productService.queryAllProduct(1,4);
        model.addAttribute("products", pageInfo);

        PageInfo<Product> pageInfo01 = productService.queryAddressProduct(1,6,priductaddress);
        model.addAttribute("addressp01",pageInfo01);

        PageInfo<Product> pageInfo02 = productService.queryAddressProduct(1,6,priductaddress01);
        model.addAttribute("addressp02",pageInfo02);

        PageInfo<Product> pageInfo03 = productService.queryAddressProduct(1,6,priductaddress02);
        model.addAttribute("addressp03",pageInfo03);

        PageInfo<Product> pageInfo04 = productService.queryAddressProduct(1,6,priductaddress03);
        model.addAttribute("addressp04",pageInfo04);


        return "front_end/travel";
    }

    //商品团购主页
    @RequestMapping("/travel_team")
    public String  travel_team(String number01,
                               String number02,
                               String number03,
                               String number04,
                          Model model,
                          HttpSession session){

        if (session.getAttribute("users")==null){
            return "userLogin";
        }

        PageInfo<Product_team> pageInfo = productTeamService.queryAllProduct(1,4);
        model.addAttribute("products", pageInfo);

        PageInfo<Product_team> pageInfo01 = productTeamService.queryNumberProduct(1,6,number01);
        model.addAttribute("addressp01",pageInfo01);

        PageInfo<Product_team> pageInfo02 = productTeamService.queryNumberProduct(1,6,number02);
        model.addAttribute("addressp02",pageInfo02);

        PageInfo<Product_team> pageInfo03 = productTeamService.queryNumberProduct(1,6,number03);
        model.addAttribute("addressp03",pageInfo03);

        PageInfo<Product_team> pageInfo04 = productTeamService.queryNumberProduct(1,6,number04);
        model.addAttribute("addressp04",pageInfo04);




        return "front_end/travel_team";
    }

    //查询所有商品
    @RequestMapping("/allproduct")
    public String allproduct(Model model){
        List<Product> allproduct = productService.allproduct();
        model.addAttribute("allproduct",allproduct);
        return "front_end/travel_allproduct";
    }



    //单个地区多项目页
    @RequestMapping("/one_travel_province")
    public String one_travel_province(String priductaddress,
                                  Model model){
        PageInfo<Product> pageInfo01 = productService.queryAddressProduct(1,50,priductaddress);
        model.addAttribute("addressp01",pageInfo01);
        model.addAttribute("address",priductaddress);



        return "front_end/travel_province";
    }

    //详情页
    @RequestMapping("/travel_details")
    public String travel_details(String productcode,Model model){

        Product product = productService.quryOenProduct(productcode);
        model.addAttribute("product",product);

        return "front_end/travel_details";
    }

    //用户查看详情
    @RequestMapping("/details")
    public String details(int cno,String productcode,Model model){


        if (cno==1) {
            Product detail = productService.detail(productcode);
            System.out.println("1111111111111111111111111111111" + detail);
            model.addAttribute("product", detail);

            return "front_end/travel_details";
        }else {
            Product_team detail = productTeamService.detail(productcode);
            model.addAttribute("product",detail);
            return "front_end/travel_teamdetails";
        }
    }





    //团购详情页
    @RequestMapping("/travel_teamdetails")
    public String travel_teamdetails(String productcode,Model model){

        Product_team product = productTeamService.quryOenProduct(productcode);
        model.addAttribute("product",product);

        return "front_end/travel_teamdetails";
    }

    //用户订单页
    @RequestMapping("/usersOrder")
    public String usersOrder(){





        return "front_end/order";
    }


    //购买
    @RequestMapping("/buy1")
    public String buy1(String Child_number,String Adult_number,String total_price,String ordername,String usercode,HttpSession session,String productcode) throws ParseException {



        System.out.println(Child_number);
        System.out.println(Adult_number);
        System.out.println(total_price);
        System.out.println(ordername);
        System.out.println(usercode);
        System.out.println(productcode);



        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取String类型的时间
        String createdate = sdf.format(date);
        System.out.println(createdate);

        SimpleDateFormat qw = new SimpleDateFormat("yyyy-MM-dd");
        Date qwer = qw.parse(createdate);
        System.out.println(qwer);



        Users users = (Users) session.getAttribute("users");
        String name=users.getName();
        int cno=1;



        orderService.addOrder(ordername,qwer,usercode,Child_number,Adult_number,total_price,name,productcode,cno);
        usersService.updateMoney(name,total_price);
        userService.addMoney(usercode,total_price);
        productService.updateremain_number(productcode,Adult_number,Child_number);
        productTeamService.updateremain_number(productcode,Adult_number,Child_number);



        return "front_end/order";
    }



    //购买
    @RequestMapping("/buy2")
    public String buy2(String Child_number,String Adult_number,String total_price,String ordername,String usercode,HttpSession session,String productcode) throws ParseException {



        System.out.println(Child_number);
        System.out.println(Adult_number);
        System.out.println(total_price);
        System.out.println(ordername);
        System.out.println(usercode);
        System.out.println(productcode);



        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取String类型的时间
        String createdate = sdf.format(date);
        System.out.println(createdate);

        SimpleDateFormat qw = new SimpleDateFormat("yyyy-MM-dd");
        Date qwer = qw.parse(createdate);
        System.out.println(qwer);



        Users users = (Users) session.getAttribute("users");
        String name=users.getName();

        int cno=2;

        orderService.addOrder(ordername,qwer,usercode,Child_number,Adult_number,total_price,name,productcode,cno);
        usersService.updateMoney(name,total_price);
        userService.addMoney(usercode,total_price);
        productService.updateremain_number(productcode,Adult_number,Child_number);
        productTeamService.updateremain_number(productcode,Adult_number,Child_number);



        return "front_end/order";
    }



    //查询所有团队项目
    @RequestMapping("/travel_allteamproduct")
    public String travel_allteamproduct(Model model){
        List<Product_team> allteamproduct = productTeamService.allteamproduct();
        model.addAttribute("allteamproduct",allteamproduct);
        return "front_end/travel_allteamproduct";
    }


    //根据团购人数查询
    @RequestMapping("/team_number")
    public String team_number(String group_number,
                                      Model model){
        List<Product_team> team_number = productTeamService.team_number(group_number);
        model.addAttribute("team_number",team_number);
        model.addAttribute("group_number",group_number);



        return "front_end/travel_teamprovince";
    }

    //项目查询
    @RequestMapping("/select")
    public String select(String keyWord,Model model){

        List<Product> select = productService.select(keyWord);


            model.addAttribute("select",select);
            return "front_end/travel_select";






    }

}
